<template>
   
  <div>
      
    <div
      class="wheel-and-hamster"
      role="img"
      aria-label="Orange and tan hamster running in a metal wheel"
    >
      <div class="wheel"></div>
      <div class="hamster">
        <div class="hamster__body">
          <div class="hamster__head">
            <div class="hamster__ear"></div>
            <div class="hamster__eye"></div>
            <div class="hamster__nose"></div>
          </div>
          <div class="hamster__limb hamster__limb--fr"></div>
          <div class="hamster__limb hamster__limb--fl"></div>
          <div class="hamster__limb hamster__limb--br"></div>
          <div class="hamster__limb hamster__limb--bl"></div>
          <div class="hamster__tail"></div>
        </div>
      </div>
      <div class="spoke"></div>
    </div>
    <div class="container">
      <canvas id="canvas" width="320" height="480" tabindex="0"></canvas>
      <pre id="score">0</pre>
      <pre id="instruction">
    Press "Enter" key to start the game.
    Controls:
    "Left" Arrow key
    "Right" Arrow key
  </pre
      >
      <pre id="lose">You lose! Try again?</pre>
    </div>
  </div>
   
</template>

<script>
export default {
  props: {},
  data() {
    return {};
  },
  computed: {},
  created() {},
  mounted() {
    "use strict";

    const data = {
      fps: 25,
      speed: 25,
      speedDiff: 10,
      canvas: {
        width: 320,
        height: 480,
      },
      car1: {
        width: 80,
        height: 120,
        turn: 35,
        image: base64ToImg(
          ""
        ),
        position: {
          x: 120,
          y: 340,
        },
      },
      car2: {
        width: 80,
        height: 120,
        distance: 320,
        image: base64ToImg(
          ""
        ),
      },
      line: {
        width: 10,
        height: 50,
        distance: 100,
        pos1: 105,
        pos2: 205,
        color: "#efefef",
      },
      moves: [20, 120, 220],
      score: 1,
      grace: 10,
      increaseSpeedTime: 5000,
    };
    var game = false;
    var die = false;
    var car1Move = 0;
    var car1Turn = 0;
    var score = 0;
    var increaseSpeed = -1;
    var lines = [];
    var car1 = [];
    var car2 = [];
    var canvas = document.querySelector("#canvas");
    var context = canvas.getContext("2d");
    var instruction = document.querySelector("#instruction");
    var scoreText = document.querySelector("#score");
    var lose = document.querySelector("#lose");

    window.addEventListener("keydown", function (evt) {
      evt.preventDefault();

      if (!game) {
        if (evt.keyCode === 13 || evt.keyCode === 32) {
          game = true;
          instruction.style.display = "none";
          lose.style.display = "none";

          initialize();
        }
      }

      if (car1Move <= 0 && car1Turn <= 0) {
        if (evt.keyCode === 37) {
          if (car1[0] === data.moves[1]) {
            car1Move = data.moves[0];
            car1Turn = 1;
          } else if (car1[0] === data.moves[2]) {
            car1Move = data.moves[1];
            car1Turn = 1;
          }
        } else if (evt.keyCode === 39) {
          if (car1[0] === data.moves[0]) {
            car1Move = data.moves[1];
            car1Turn = 2;
          } else if (car1[0] === data.moves[1]) {
            car1Move = data.moves[2];
            car1Turn = 2;
          }
        }
      }
    });

    function base64ToImg(value) {
      var img = new Image();
      img.src = value;
      return img;
    }

    function clearCanvas() {
      context.clearRect(0, 0, data.canvas.width, data.canvas.height);
    }

    function initLines() {
      var y = data.canvas.height - data.line.height;

      while (y >= 0) {
        lines.push([
          [data.line.pos1, y, data.line.width, data.line.height],
          [data.line.pos2, y, data.line.width, data.line.height],
        ]);

        y -= data.line.distance;
      }
    }

    function initCar1() {
      car1 = [
        data.car1.position.x,
        data.car1.position.y,
        data.car1.width,
        data.car1.height,
      ];
    }

    function initCar2() {
      for (var i = 0; i < Math.floor(Math.random() * 2) + 1; i++) {
        var random = data.moves[Math.floor(Math.random() * 3)];

        if (car2.length > 0) {
          if (random === car2[car2.length - 1][1]) {
            i--;
          } else {
            car2.push([random, data.car2.height * -1]);
          }
        } else {
          car2.push([random, data.car2.height * -1]);
        }
      }
    }

    function drawLines() {
      var remove = false;

      context.fillStyle = data.line.color;

      for (var i = 0; i < lines.length; i++) {
        context.fillRect(
          lines[i][0][0],
          lines[i][0][1],
          lines[i][0][2],
          lines[i][0][3]
        );
        context.fillRect(
          lines[i][1][0],
          lines[i][1][1],
          lines[i][1][2],
          lines[i][1][3]
        );

        if (lines[i][0][1] > data.canvas.height) {
          remove = i;
        } else {
          lines[i][0][1] += data.speed + data.speedDiff + increaseSpeed;
          lines[i][1][1] += data.speed + data.speedDiff + increaseSpeed;
        }
      }

      if (
        lines[lines.length - 1][0][1] >
        data.line.distance - data.line.height
      ) {
        lines.push([
          [
            data.line.pos1,
            data.line.height * -1,
            data.line.width,
            data.line.height,
          ],
          [
            data.line.pos2,
            data.line.height * -1,
            data.line.width,
            data.line.height,
          ],
        ]);
      }

      if (remove) {
        lines.splice(remove, 1);
      }
    }

    function drawCar1() {
      if (car1Move > 0 && car1Turn > 0) {
        if (car1Turn === 1) {
          if (car1[0] > car1Move) {
            car1[0] -= data.car1.turn;
          } else {
            car1[0] = car1Move;
            car1Move = 0;
            car1Turn = 0;
          }
        } else if (car1Turn === 2) {
          if (car1[0] < car1Move) {
            car1[0] += data.car1.turn;
          } else {
            car1[0] = car1Move;
            car1Move = 0;
            car1Turn = 0;
          }
        }
      }
      context.drawImage(data.car1.image, car1[0], car1[1], car1[2], car1[3]);
    }

    function drawCar2() {
      var remove = false;

      for (var i = 0; i < car2.length; i++) {
        if (car2[i][1] > data.canvas.height) {
          remove = i;
        } else {
          car2[i][1] += data.speed - data.speedDiff + increaseSpeed;
        }
        context.drawImage(
          data.car2.image,
          car2[i][0],
          car2[i][1],
          data.car2.width,
          data.car2.height
        );
      }

      if (car2[car2.length - 1][1] > data.car2.distance) {
        initCar2();
      }

      if (remove) {
        car2.splice(remove, 1);
      }
    }

    function collision() {
      for (var i = 0; i < car2.length; i++) {
        if (
          car1[0] + data.grace <= car2[i][0] + data.car2.width &&
          car1[0] + data.car1.width - data.grace >= car2[i][0] &&
          car1[1] + data.grace <= car2[i][1] + data.car2.height &&
          car1[1] + data.car2.height - data.grace >= car2[i][1]
        ) {
          die = true;
          game = false;
          instruction.style.display = "block";
          lose.style.display = "block";
        }
      }
    }

    function incrementScore() {
      score += data.score;

      scoreText.innerHTML = score;
    }

    function render(callback) {
      setTimeout(function () {
        requestAnimationFrame(render);

        if (
          (Math.round(performance.now() / 100) * 100) %
            data.increaseSpeedTime ===
          0
        ) {
          increaseSpeed++;
        }

        if (!die && game) {
          clearCanvas();
          drawLines();
          drawCar1();
          drawCar2();
          collision();
          incrementScore();
        }
      }, 1000 / data.fps);
    }

    function initialize() {
      die = false;
      score = 0;
      car1Move = 0;
      car1Turn = 0;
      increaseSpeed = -1;
      lines = [];
      car1 = [];
      car2 = [];

      initLines();
      initCar1();
      initCar2();
      clearCanvas();
    }

    render();
  },
  watch: {},
  methods: {},
  components: {},
};
</script>

<style scoped lang="scss">
* {
  border: 0;
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}
:root {
  --bg: hsl(90, 90%, 20%);
  --fg: hsl(223, 10%, 10%);
  font-size: calc(16px + (24 - 16) * (100vw - 320px) / (1280 - 320));
}
body {
  background: var(--bg);
  color: var(--fg);
  font: 1em/1.5 sans-serif;
  height: 100vh;
  display: grid;
  place-items: center;
}
.wheel-and-hamster {
  --dur: 1s;
  position: relative;
  width: 12em;
  height: 12em;
}
.wheel,
.hamster,
.hamster div,
.spoke {
  position: absolute;
}
.wheel,
.spoke {
  border-radius: 50%;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
}
.wheel {
  background: radial-gradient(
    100% 100% at center,
    hsla(0, 0%, 60%, 0) 47.8%,
    hsl(0, 0%, 60%) 48%
  );
  z-index: 2;
}
.hamster {
  animation: hamster var(--dur) ease-in-out infinite;
  top: 50%;
  left: calc(50% - 3.5em);
  width: 7em;
  height: 3.75em;
  transform: rotate(4deg) translate(-0.8em, 1.85em);
  transform-origin: 50% 0;
  z-index: 1;
}
.hamster__head {
  animation: hamsterHead var(--dur) ease-in-out infinite;
  background: hsl(30, 90%, 55%);
  border-radius: 70% 30% 0 100% / 40% 25% 25% 60%;
  box-shadow: 0 -0.25em 0 hsl(30, 90%, 80%) inset,
    0.75em -1.55em 0 hsl(30, 90%, 90%) inset;
  top: 0;
  left: -2em;
  width: 2.75em;
  height: 2.5em;
  transform-origin: 100% 50%;
}
.hamster__ear {
  animation: hamsterEar var(--dur) ease-in-out infinite;
  background: hsl(0, 90%, 85%);
  border-radius: 50%;
  box-shadow: -0.25em 0 hsl(30, 90%, 55%) inset;
  top: -0.25em;
  right: -0.25em;
  width: 0.75em;
  height: 0.75em;
  transform-origin: 50% 75%;
}
.hamster__eye {
  animation: hamsterEye var(--dur) linear infinite;
  background-color: hsl(0, 0%, 0%);
  border-radius: 50%;
  top: 0.375em;
  left: 1.25em;
  width: 0.5em;
  height: 0.5em;
}
.hamster__nose {
  background: hsl(0, 90%, 75%);
  border-radius: 35% 65% 85% 15% / 70% 50% 50% 30%;
  top: 0.75em;
  left: 0;
  width: 0.2em;
  height: 0.25em;
}
.hamster__body {
  animation: hamsterBody var(--dur) ease-in-out infinite;
  background: hsl(30, 90%, 90%);
  border-radius: 50% 30% 50% 30% / 15% 60% 40% 40%;
  box-shadow: 0.1em 0.75em 0 hsl(30, 90%, 55%) inset,
    0.15em -0.5em 0 hsl(30, 90%, 80%) inset;
  top: 0.25em;
  left: 2em;
  width: 4.5em;
  height: 3em;
  transform-origin: 17% 50%;
  transform-style: preserve-3d;
}
.hamster__limb--fr,
.hamster__limb--fl {
  clip-path: polygon(0 0, 100% 0, 70% 80%, 60% 100%, 0% 100%, 40% 80%);
  top: 2em;
  left: 0.5em;
  width: 1em;
  height: 1.5em;
  transform-origin: 50% 0;
}
.hamster__limb--fr {
  animation: hamsterFRLimb var(--dur) linear infinite;
  background: linear-gradient(hsl(30, 90%, 80%) 80%, hsl(0, 90%, 75%) 80%);
  transform: rotate(15deg) translateZ(-1px);
}
.hamster__limb--fl {
  animation: hamsterFLLimb var(--dur) linear infinite;
  background: linear-gradient(hsl(30, 90%, 90%) 80%, hsl(0, 90%, 85%) 80%);
  transform: rotate(15deg);
}
.hamster__limb--br,
.hamster__limb--bl {
  border-radius: 0.75em 0.75em 0 0;
  clip-path: polygon(
    0 0,
    100% 0,
    100% 30%,
    70% 90%,
    70% 100%,
    30% 100%,
    40% 90%,
    0% 30%
  );
  top: 1em;
  left: 2.8em;
  width: 1.5em;
  height: 2.5em;
  transform-origin: 50% 30%;
}
.hamster__limb--br {
  animation: hamsterBRLimb var(--dur) linear infinite;
  background: linear-gradient(hsl(30, 90%, 80%) 90%, hsl(0, 90%, 75%) 90%);
  transform: rotate(-25deg) translateZ(-1px);
}
.hamster__limb--bl {
  animation: hamsterBLLimb var(--dur) linear infinite;
  background: linear-gradient(hsl(30, 90%, 90%) 90%, hsl(0, 90%, 85%) 90%);
  transform: rotate(-25deg);
}
.hamster__tail {
  animation: hamsterTail var(--dur) linear infinite;
  background: hsl(0, 90%, 85%);
  border-radius: 0.25em 50% 50% 0.25em;
  box-shadow: 0 -0.2em 0 hsl(0, 90%, 75%) inset;
  top: 1.5em;
  right: -0.5em;
  width: 1em;
  height: 0.5em;
  transform: rotate(30deg) translateZ(-1px);
  transform-origin: 0.25em 0.25em;
}
.spoke {
  animation: spoke var(--dur) linear infinite;
  background: radial-gradient(
      100% 100% at center,
      hsl(0, 0%, 60%) 4.8%,
      hsla(0, 0%, 60%, 0) 5%
    ),
    linear-gradient(
        hsla(0, 0%, 55%, 0) 46.9%,
        hsl(0, 0%, 65%) 47% 52.9%,
        hsla(0, 0%, 65%, 0) 53%
      )
      50% 50% / 99% 99% no-repeat;
}

/* Animations */
@keyframes hamster {
  from,
  to {
    transform: rotate(4deg) translate(-0.8em, 1.85em);
  }
  50% {
    transform: rotate(0) translate(-0.8em, 1.85em);
  }
}
@keyframes hamsterHead {
  from,
  25%,
  50%,
  75%,
  to {
    transform: rotate(0);
  }
  12.5%,
  37.5%,
  62.5%,
  87.5% {
    transform: rotate(8deg);
  }
}
@keyframes hamsterEye {
  from,
  90%,
  to {
    transform: scaleY(1);
  }
  95% {
    transform: scaleY(0);
  }
}
@keyframes hamsterEar {
  from,
  25%,
  50%,
  75%,
  to {
    transform: rotate(0);
  }
  12.5%,
  37.5%,
  62.5%,
  87.5% {
    transform: rotate(12deg);
  }
}
@keyframes hamsterBody {
  from,
  25%,
  50%,
  75%,
  to {
    transform: rotate(0);
  }
  12.5%,
  37.5%,
  62.5%,
  87.5% {
    transform: rotate(-2deg);
  }
}
@keyframes hamsterFRLimb {
  from,
  25%,
  50%,
  75%,
  to {
    transform: rotate(50deg) translateZ(-1px);
  }
  12.5%,
  37.5%,
  62.5%,
  87.5% {
    transform: rotate(-30deg) translateZ(-1px);
  }
}
@keyframes hamsterFLLimb {
  from,
  25%,
  50%,
  75%,
  to {
    transform: rotate(-30deg);
  }
  12.5%,
  37.5%,
  62.5%,
  87.5% {
    transform: rotate(50deg);
  }
}
@keyframes hamsterBRLimb {
  from,
  25%,
  50%,
  75%,
  to {
    transform: rotate(-60deg) translateZ(-1px);
  }
  12.5%,
  37.5%,
  62.5%,
  87.5% {
    transform: rotate(20deg) translateZ(-1px);
  }
}
@keyframes hamsterBLLimb {
  from,
  25%,
  50%,
  75%,
  to {
    transform: rotate(20deg);
  }
  12.5%,
  37.5%,
  62.5%,
  87.5% {
    transform: rotate(-60deg);
  }
}
@keyframes hamsterTail {
  from,
  25%,
  50%,
  75%,
  to {
    transform: rotate(30deg) translateZ(-1px);
  }
  12.5%,
  37.5%,
  62.5%,
  87.5% {
    transform: rotate(10deg) translateZ(-1px);
  }
}
@keyframes spoke {
  from {
    transform: rotate(0);
  }
  to {
    transform: rotate(-1turn);
  }
}
</style>

<style scoped lang="scss">
.container {
  position: absolute;
  top: calc(150% - 240px);
  left: calc(50% - 160px);
}

#canvas {
  background: url("");
  margin: 0 auto;
  outline: none;
}

#score {
  font-size: 20px;
  font-weight: bold;
  color: white;
  display: block;
  position: absolute;
  top: 10px;
  left: 10px;
  text-shadow: -2px 0 black, -2px 2px black, -2px -2px black, 0 2px black,
    2px 0 black, 2px 2px black, 2px -2px black, 0 -2px black;
}

#instruction {
  width: 100%;
  font-size: 14px;
  font-weight: bold;
  color: white;
  display: block;
  position: absolute;
  top: 200px;
  left: 0;
  text-shadow: -2px 0 black, -2px 2px black, -2px -2px black, 0 2px black,
    2px 0 black, 2px 2px black, 2px -2px black, 0 -2px black;
}

#lose {
  width: 100%;
  font-size: 14px;
  font-weight: bold;
  color: white;
  display: none;
  position: absolute;
  top: 150px;
  left: 30px;
  text-shadow: -2px 0 black, -2px 2px black, -2px -2px black, 0 2px black,
    2px 0 black, 2px 2px black, 2px -2px black, 0 -2px black;
}
</style>

<style scoped lang="scss"></style>
<style scoped lang="scss"></style>
<style scoped lang="scss"></style>
<style scoped lang="scss"></style>
<style scoped lang="scss"></style>
